package com.cuit.qx.controller;


import com.cuit.qx.aop.OperLog;
import com.cuit.qx.entity.Product;
import com.cuit.qx.entity.RiskWarning;
import com.cuit.qx.service.RiskWarningService;
import com.cuit.qx.utils.ReturnData;
import com.cuit.qx.utils.ReturnStateCode;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cuit.qx.entity.CorrectForecast;
import com.cuit.qx.entity.RiskWarning;
import com.cuit.qx.service.RiskWarningService;
import com.cuit.qx.utils.JsonUtil;
import com.cuit.qx.utils.ReturnData;
import com.cuit.qx.utils.ReturnStateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 风险预警
 * </p>
 *
 * @author lcj
 * @since 2021-11-23
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/riskWarning")
public class RiskWarningController {

    @Resource
    private RiskWarningService riskWarningService;

    @PostMapping("/list")
    @OperLog(operModul = "风险预警数据查询", operType = "1", operDesc = "查询风险预警数据")
    public ReturnData getList(@RequestBody Map<String, Object> map) {
        List<RiskWarning> list = new ArrayList<>();
        try {
            String pattern = map.get("pattern").toString();
            String date = map.get("date").toString();
            String startTime = map.get("startTime").toString();
            String moment = map.get("moment").toString();
            String stationName = (String) map.get("stationName");

            list = riskWarningService.getList(pattern, date, startTime, moment,stationName);
        } catch (Exception e) {
            return new ReturnData(ReturnStateCode.FAILEDCODE);
        }

        if (list.isEmpty()) {
            return new ReturnData(ReturnStateCode.SUCCEEBUT);
        }
        return new ReturnData(ReturnStateCode.SUCCEE, list);
    }

    @PostMapping("/makeProduce")
    @OperLog(operModul = "产品制作", operType = "1", operDesc = "风险预警产品制作")
    public ReturnData makeProduct(@RequestBody Map<String, Object> map) {
        try {
            String pattern = map.get("pattern").toString();
            String date = map.get("date").toString();
            String startTime = map.get("startTime").toString();
            String moment = map.get("moment").toString();
            String stationName = (String) map.get("stationName");

            String forecastUsr = map.get("forecastUsr").toString();
            String enteringUsr = map.get("enteringUsr").toString();
            String verifyUsr = map.get("verifyUsr").toString();
            String department = map.get("department").toString();
            Product product = new Product().setForecastUsr(forecastUsr)
                    .setEnteringUsr(enteringUsr)
                    .setVerifyUsr(verifyUsr)
                    .setDepartment(department)
                    .setProductMoment(Integer.valueOf(moment))
                    .setPattern(pattern);

            ReturnData result = riskWarningService.makeProduct(pattern, date, startTime, moment,stationName, product);

            return result;
        } catch (NullPointerException ex){
            return new ReturnData(ReturnStateCode.FAILEDCODE);
        }catch (Exception e) {
            return new ReturnData(ReturnStateCode.FAILED,e.getMessage());
        }

    }

    @Autowired
    private RiskWarningService service;
    /**
     * @description: 根据条件查询暴雨风险预警
     * @return:
     * @author: wjd
     * @time: 2021/11/23 19:18
     */
    @RequestMapping("/getRiskWarning")
    public ReturnData getRiskWarningData(int pattern, String startDate, String startTime, String moment){

        List<RiskWarning> riskWarnin = service.getRiskWarnin(pattern, startDate, startTime, moment);

        return new ReturnData(ReturnStateCode.SUCCEE.getCode(),ReturnStateCode.SUCCEE.getMsg(),riskWarnin);
    }
    /**
     * @description: 修改预警信息值以及风险等级，目前所有阈值都为55mm,待后期用户
     * 提供等级、或者不同阈值再进行优化。
     * @return:
     * @author: wjd
     * @time: 2021/11/24 10:22
     */
    @RequestMapping(value = "/updateWarningU",method = RequestMethod.POST)
    public ReturnData updateWarningU(@RequestBody  List<RiskWarning> warnings){
        return service.updateWarningU(warnings);
    }

}
